package com.pzhu.dormitory.utils;


import java.util.List;

/**
 * @author liupeng
 */
public final class Rb<T> {
    private Boolean success;
    private Long ts = System.currentTimeMillis();
    private T data;
    /**
     * 分页数据
     */
    private PageUtils<T> page;
    /**
     * list 数据
     */
    private List<T> list;

    private Integer code;
    private String msg;

    private Rb() {
    }

    public static <R> Rb<R> a(Class<R> r) {
        return new Rb<>();
    }

    public static Rb<?> a() {
        return new Rb<>();
    }

    public Rb<T> success() {
        this.success = true;
        this.code = 200;
        this.msg = "操作成功";
        return this;
    }

    public Rb<T> fail() {
        this.success = false;
        this.msg = "操作失败";
        return this;
    }

    public Rb<T> withSuccess(Boolean success) {
        this.success = success;
        this.code = 200;
        return this;
    }

    public Rb<T> withTs(Long ts) {
        this.ts = ts;
        return this;
    }

    public Rb<T> withData(T data) {
        this.data = data;
        return this;
    }

    public Rb<T> withCode(Integer code) {
        this.code = code;
        return this;
    }

    public Rb<T> withMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Rb<T> list(List<T> list) {
        this.list = list;
        return this;
    }

    public Rb<T> page(PageUtils<T> page) {
        this.page = page;
        return this;
    }


    public R<T> build() {
        R<T> result = new R<>();
        result.setSuccess(success);
        result.setTs(ts);
        result.setData(data);
        result.setList(list);
        result.setPage(page);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
